package m

import (
	"GameServer/Game/dbmodule"

	"github.com/globalsign/mgo/bson"
)

type EventMsg struct {
	Id          string `bson:"id" json:"id"`
	Uid         string `bson:"uid" json:"uid"`
	Name        string `bson:"name" json:"name"`
	Head        string `bson:"head" json:"head"`
	Op          int64  `bson:"op" json:"op"`
	Value       int64  `bson:"value"`
	Time        int64  `bson:"time"`
	AvatarFrame int64  `bson:"avatarFrame" json:"avatarFrame"`
}

func AddEventMsg(uid bson.ObjectId, eMsg *EventMsg) (err error) {
	err = dbmodule.MgoDB.GameDB.Upsert("eventMsg", bson.M{"_id": uid}, bson.M{"$push": bson.M{"eventMsgList": bson.M{"$each": []*EventMsg{eMsg}, "$slice": -30}}})
	return
}

type UserEventMsg struct {
	Uid          bson.ObjectId `bson:"_id" json:"id"`
	EventMsgList []*EventMsg   `bson:"eventMsgList" json:"eventMsgList"`
}

func NewUserEventMsg(uid bson.ObjectId) *UserEventMsg {
	return &UserEventMsg{Uid: uid, EventMsgList: make([]*EventMsg, 0)}
}

func (ueMsg *UserEventMsg) GetEventMsgList() (r []*EventMsg, err error) {
	err = dbmodule.MgoDB.GameDB.FindOne("eventMsg", bson.M{"_id": ueMsg.Uid}, nil, &ueMsg)
	if err != nil {
		if err.Error() == "not found" {
			return r, nil
		}
		return
	}
	r = ueMsg.EventMsgList
	return
}
